package com.fourth.util;

import com.fourth.dao.UbCommentDao;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/3/3 15:24
 **/
@Component
public class GetUidUtil {

    private static UbCommentDao ubCommentDao;

    @Autowired
    public GetUidUtil(UbCommentDao ubCommentDao) {
        GetUidUtil.ubCommentDao = ubCommentDao;
    }

    /**
     * 获取当前登录用户的用户名
     * @return
     */
    public static String getUid(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String username = tokenChaim.get("username").toString();
        String userid = ubCommentDao.getUserIdByUsername(username);
        return userid;
    }

}
